

#include <SchedulerARMAVR.h>
#include <SCoop.h>

defineTask(TaskTest1);
defineTask(TaskTest2);

const unsigned char
dofly_DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; 
const unsigned char
dofly_DuanMa2[10]={0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF};
//有小数点的段码
int CLKsecond = 8;
int DATsecondhigh = 9;
int DATsecondlow = 10;
int CLKminute = 5;
int DATminutehigh = 6;
int DATminutelow = 7;
int CLKhour = 2;
int DAThourhigh = 3;
int DAThourlow = 4;
int second = 0;
int minute = 0;
int hour = 0;
int LEDsecond = 11;
int LEDminute = 12;
int LEDhour = A1;
int switchread = A0;
int debouncetime = 50;
int setcondition = 0;
int clocksecondlow = 0;
int clocksecondhigh = 0;
int clockminute = 0;
int clockcondition = 0;
int begincondition = 1;
unsigned long lasttime = 0;
unsigned currenttime = 0;
bool oncetime = true;
void SendByte(unsigned char dathigh,unsigned char datlow,char data) 
{ 
 static unsigned char i; 
 if(data=='S')
 {
  for(i=0;i<8;i++) 
  { 
   digitalWrite(CLKsecond,0); 
   digitalWrite(DATsecondhigh,bitRead(dathigh,7-i));
   digitalWrite(DATsecondlow,bitRead(datlow,7-i));
   digitalWrite(CLKsecond,1); 
  } 
 }
 
 if(data=='M')
 {
  for(i=0;i<8;i++) 
  { 
   digitalWrite(CLKminute,0); 
   digitalWrite(DATminutehigh,bitRead(dathigh,7-i));
   digitalWrite(DATminutelow,bitRead(datlow,7-i));
   digitalWrite(CLKminute,1); 
  } 
 }

 if(data=='H')
 {
  for(i=0;i<8;i++) 
  { 
   digitalWrite(CLKhour,0); 
   digitalWrite(DAThourhigh,bitRead(dathigh,7-i));
   digitalWrite(DAThourlow,bitRead(datlow,7-i));
   digitalWrite(CLKhour,1); 
  } 
 }
 
} 

void TimeSetsecond()
{
  int reading3 = analogRead(switchread);
  //sleep(debouncetime);
  int reading4 = analogRead(switchread);
  if(reading4-reading3 >=-1 && reading4-reading3<=1)
  {
    if(reading4 >= 500 && reading4 < 1000)
      second = 0;
  }
  SendByte(dofly_DuanMa[second/10],dofly_DuanMa[second%10],'S');
}

void TimeSetminute()
{
  int reading3 = analogRead(switchread);
  //sleep(debouncetime);
  int reading4 = analogRead(switchread);
  if(reading4-reading3 >=-1 && reading4-reading3<=1)
  {
    if(reading4 >= 500 && reading4 < 1000)
      minute++;
      if(minute==60)
      minute = 0;
  }
  SendByte(dofly_DuanMa[minute/10],dofly_DuanMa2[minute%10],'M');
}

void TimeSethour()
{
  int reading3 = analogRead(switchread);
  //sleep(debouncetime);
  int reading4 = analogRead(switchread);
  if(reading4-reading3 >=-1 && reading4-reading3<=1)
  {
    if(reading4 >= 500 && reading4 < 1000)
      hour++;
      if(hour == 24)
      hour = 0;
  }
  SendByte(dofly_DuanMa[hour/10],dofly_DuanMa2[hour%10],'H');
}

void clearclock()
{
  clockminute = 0;
  clocksecondhigh = 0;
  clocksecondlow = 0;
}


void TaskTest1::setup() {
 pinMode(CLKsecond, OUTPUT); 
 pinMode(DATsecondhigh, OUTPUT);
 pinMode(DATsecondlow, OUTPUT);
 pinMode(CLKminute, OUTPUT); 
 pinMode(DATminutehigh, OUTPUT);
 pinMode(DATminutelow, OUTPUT);
 pinMode(CLKhour, OUTPUT); 
 pinMode(DAThourhigh, OUTPUT);
 pinMode(DAThourlow, OUTPUT);
 pinMode(LEDsecond,OUTPUT);
 pinMode(LEDminute,OUTPUT);
 pinMode(LEDhour,OUTPUT);
 pinMode(switchread,INPUT);
 digitalWrite(LEDsecond,HIGH);
 digitalWrite(LEDminute,HIGH);
 digitalWrite(LEDhour,HIGH);
 Serial.begin(9600);
}


void TaskTest1::loop() {
  int reading1 = analogRead(switchread);
  //sleep(debouncetime);
  int reading2 = analogRead(switchread);
  Serial.println(reading1);
  Serial.println(reading2);
  Serial.println(setcondition);
   if(clocksecondlow==100)
   {
   clocksecondlow = 0;
   clocksecondhigh++;
   }
   if(clocksecondhigh == 60)
   {
   clocksecondhigh = 0;
   clockminute++;
   }
   if(clockminute == 60)
   clockminute = 0;
        
  if(second == 60)
  {
  second = 0;
  minute++;
  }
  if(minute == 60)
  {
  minute = 0;
  hour++;
  }
  if(hour == 24)
  hour = 0;
  
  if(reading2 - reading1 >=-1 && reading2 - reading1 <= 1)
  {
    if(reading2==0)
      goto FLAG;
    if(reading2 >= 1000)
    {
      setcondition++;
      if(setcondition==4)
      setcondition = 0;
    }
    if(reading2 >= 300 && reading2 < 500)
    {
      //sleep(debouncetime);
      setcondition = 5;
      Serial.println("clock!");
      clockcondition++;
      if(clockcondition == 2)
      {
      clockcondition = 0;
      setcondition = 0;
      }
    }
    if(reading2 >= 200 && reading2 < 300)
    {
      begincondition = -begincondition;
      //sleep(debouncetime);
    }
    if(reading2 >= 500 && reading2 < 1000 && clockcondition == 1)
    {
      clearclock();

    }
        
  }
  switch(setcondition)
      {
        case 0:
        Serial.println("OK!");
        digitalWrite(LEDsecond,LOW);
        digitalWrite(LEDminute,LOW);
        digitalWrite(LEDhour,LOW);
        clearclock();
        oncetime = true;
        break;
        case 1:
        Serial.println("OK!");
        digitalWrite(LEDsecond,LOW);
        digitalWrite(LEDminute,LOW);
        digitalWrite(LEDhour,HIGH);
        TimeSethour();
        break;
        case 2:
        Serial.println("OK!");
        digitalWrite(LEDsecond,LOW);
        digitalWrite(LEDminute,HIGH);
        digitalWrite(LEDhour,LOW);
        TimeSetminute();
        break;
        case 3:
        Serial.println("OK!");
        digitalWrite(LEDsecond,HIGH);
        digitalWrite(LEDminute,LOW);
        digitalWrite(LEDhour,LOW);
        TimeSetsecond();
        break;
      }
      
  FLAG:
  if(setcondition==0)
  {
  SendByte(dofly_DuanMa[second/10],dofly_DuanMa[second%10],'S'); 
  SendByte(dofly_DuanMa[minute/10],dofly_DuanMa2[minute%10],'M'); 
  SendByte(dofly_DuanMa[hour/10],dofly_DuanMa2[hour%10],'H'); 
  second++;
  }
  if(setcondition==5)
  {
     if(begincondition==-1)
     {
       clocksecondlow+=debouncetime/10;
     }
     SendByte(dofly_DuanMa[clocksecondlow/10],dofly_DuanMa[clocksecondlow%10],'S'); 
     SendByte(dofly_DuanMa[clocksecondhigh/10],dofly_DuanMa2[clocksecondhigh%10],'M'); 
     SendByte(dofly_DuanMa[clockminute/10],dofly_DuanMa2[clockminute%10],'H');
    
  }
  //if(setcondition ==0)
  //sleep(1000-debouncetime);
  //if(setcondition > 0 && setcondition <5)
  //sleep(debouncetime);
}
void TaskTest2::setup()
{
  ;
}
void TaskTest2::loop()
{
;
}

void setup()
{
  mySCoop.start();
}

void loop()
{
  yield();
}
